package controller;
import java.util.ArrayList;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import securiy.WebSecurityConfig;
import service.SysFuncService;
import service.SysRoleFuncService;
import service.SysUserService;


import entity.SysFunc;
import entity.SysRoleFunc;
import entity.SysUser;
/**
 * Created by wenchao.ren on 2014/4/26.
 */

//启注解事务管理  
//WebSecurityConfig.class必须要注入
@Controller
@Import({ SysFuncService.class,SysUserService.class,SysRoleFuncService.class,WebSecurityConfig.class}) 
@SpringBootApplication
@ComponentScan(basePackages = { "rabbit" })
@EnableMongoRepositories(basePackages="mongoMapper" ) 
public class SimpleController {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysFuncService sysFuncService;
	@Autowired
	private SysRoleFuncService sysRoleFuncService;
	
	@RequestMapping(value ="/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello(){
		SysUser sysUser = new SysUser();
        return "dd";
    }
	
	@RequestMapping(value ="/")
    public String home(Model model){
		//获取当前登录用户信息
		SysUser sysUser = sysUserService.getCurrentSysUser();
		List<SysFunc> sysFuncPreList = new ArrayList<>();
		Long roleId = sysUser.getRoleId();
		System.out.println("roleId========="+roleId);
		System.out.println("sysUsername========="+sysUser.getUsername());
		List<SysRoleFunc> sysRoleFuncList = sysRoleFuncService.findByRoleId(roleId);
		for(SysRoleFunc sysRoleFunc:sysRoleFuncList){
			List<SysFunc> sysFuncSub2List = new ArrayList<>();
			long funcId = sysRoleFunc.getFuncId();
			SysFunc sysFunc = sysFuncService.findById(funcId);
			if(sysFunc.getPreId()==0L){
				List<SysFunc> sysFuncSubList = sysFuncService.findByPreId(sysFunc.getSeqid());
				for(SysFunc sysFuncSub:sysFuncSubList){
					if(sysRoleFuncService.findByRoleIdAndFuncId(roleId,sysFuncSub.getSeqid())!=null){
						sysFuncSub2List.add(sysFuncSub);
					}
				}
				sysFunc.setSysFuncSubList(sysFuncSub2List);
				sysFuncPreList.add(sysFunc);
			}

		}
		
		
		
		model.addAttribute("sysFuncs", sysFuncPreList);	
    	//view.addObject("SysFuncList", SysFuncList);
    
        return "home";
    }
	
    @RequestMapping("/login")
    public String login() {
        return "login";
    }
 
   public static void main(String[] args) {
        SpringApplication.run(SimpleController.class, args);
    }
    
}
